<template>
  <div>
    <div class="project" v-if="!flightdataAddShow && !flightdataAdddetailShow">
      <!-- 筛选按钮 -->
      <div class="search-button">
        <el-button
          v-if="route.name === 'Flightdata'"
          v-hasPermi="['system:flightdata:query']"
          class="btn search"
          @click="getProjectList"
          ><el-icon class="icon" color="#fff"> <Search /> </el-icon
          >查询</el-button
        >
        <el-button
          v-if="route.name === 'FlightdataCheck'"
          v-hasPermi="['system:flightdataCheck:query']"
          class="btn search"
          @click="getProjectList"
          ><el-icon class="icon" color="#fff"> <Search /> </el-icon
          >查询</el-button
        >
        <el-button
          v-hasPermi="['system:flightdata:add']"
          class="btn search"
          @click="
            flightdataAddShow = true;
            pageType = 'add';
          "
          ><el-icon class="icon" color="#fff"> <Plus /> </el-icon
          >新增</el-button
        >
        <el-button class="btn add" @click="handleRefresh"
          ><el-icon class="icon" color="#fff"> <Refresh /> </el-icon
          >重置</el-button
        >
      </div>
      <!-- 筛选条件 -->
      <div class="search">
        <div class="search-item">
          <el-select
            filterable
            style="width: 120px"
            v-model="params.provinceId"
            placeholder="请选择省"
            clearable
            class="search-input"
            @change="handleProvince('province')"
          >
            <el-option
              v-for="dict in provinceList"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
          <el-select
            filterable
            style="width: 120px; margin: 0 5px"
            v-model="params.cityId"
            placeholder="请选择市"
            clearable
            class="search-input"
            @change="handleProvince('city')"
          >
            <el-option
              v-for="dict in cityList"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
          <el-select
            filterable
            style="width: 120px"
            v-model="params.zoneId"
            placeholder="请选择区"
            clearable
            class="search-input"
          >
            <el-option
              v-for="dict in areaList"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </div>
        <div class="search-item">
          <el-input
            clearable
            v-model.trim="params.tester"
            class="search-input"
            placeholder="请输入测试人员"
          ></el-input>
        </div>
        <div class="search-item">
          <el-input
            clearable
            v-model.trim="params.projectName"
            class="search-input"
            placeholder="请输入项目名称"
          ></el-input>
        </div>
        <div class="search-item">
          <el-select
            ref="refSelect"
            v-model="params.flyType"
            placeholder="请选择"
            clearable
            class="search-select"
          >
            <el-option
              v-for="dict in flightTypeList"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </div>
      </div>

      <!-- 表格数据 -->
      <el-table
        v-loading="loading"
        :data="projectList"
        border
        height="64vh"
        :header-cell-style="{ 'text-align': 'center' }"
      >
        <!-- 操作模式 -->
        <el-table-column
          label="操作"
          :show-overflow-tooltip="true"
          width="140px"
        >
          <template #default="{ row }">
            <div class="table-operate">
              <el-icon
                v-if="route.name === 'Flightdata'"
                v-hasPermi="['system:flightdata:detail']"
                title="查看详情"
                color="#007CEF"
                class="table-icon"
                :size="20"
                @click="handleOperate('detail', row)"
              >
                <Document />
              </el-icon>
              <el-icon
                v-if="route.name === 'FlightdataCheck'"
                v-hasPermi="['system:flightdataCheck:detail']"
                title="查看详情"
                color="#007CEF"
                class="table-icon"
                :size="20"
                @click="handleOperate('detail', row)"
              >
                <Document />
              </el-icon>
              <el-icon
                v-if="route.name === 'Flightdata'"
                v-hasPermi="['system:flightdata:edit']"
                title="编辑"
                color="#FFB700"
                class="table-icon"
                :size="20"
                @click="handleOperate('edit', row)"
              >
                <Edit />
              </el-icon>
              <el-icon
                v-if="route.name === 'FlightdataCheck'"
                v-hasPermi="['system:flightdataCheck:edit']"
                title="编辑"
                color="#FFB700"
                class="table-icon"
                :size="20"
                @click="handleOperate('edit', row)"
              >
                <Edit />
              </el-icon>
              <el-icon
                v-if="route.name === 'Flightdata'"
                v-hasPermi="['system:flightdata:remove']"
                title="删除"
                color="#E33838"
                class="table-icon"
                :size="20"
                @click="handleOperate('delete', row)"
              >
                <Delete />
              </el-icon>
            </div> </template
        ></el-table-column>
        <el-table-column
          type="index"
          label="序号"
          :index="indexMethod"
          width="70"
          align="center"
        />
        <el-table-column
          label="飞机类型"
          prop="flyType"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="省市区"
          prop="projectName"
          :show-overflow-tooltip="true"
          ><template #default="{ row }">
            {{ row.provinceName }}{{ row.cityName }}{{ row.zoneName }}
          </template>
        </el-table-column>
        <el-table-column
          label="测试人员"
          prop="userName"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          label="项目名称"
          prop="projectName"
          :show-overflow-tooltip="true"
          >
        <template #default="{ row }">
            <span>{{ row.projectName ? row.projectName : "--" }}</span>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <pagination
        class="pagination"
        :total="total"
        v-model:page="params.pageNum"
        v-model:limit="params.pageSize"
        @pagination="getProjectList"
      />
    </div>
    <flightdataAdd
      v-show="flightdataAddShow"
      ref="flightdataAddRef"
      v-model:flightdataAddShow="flightdataAddShow"
      @refreshList="getProjectList"
      :pageType="pageType"
    />
    <!-- <ServiceAdd
     v-if="ServiceAddShow"
      ref="ServiceAddRef"
      v-model:ServiceAddShow="ServiceAddShow"
      @refreshList="getProjectList"
      :pageType="pageType"
    />  -->
    <flightdataAdddetail
      v-show="flightdataAdddetailShow"
      ref="flightdataAdddetailRef"
      v-model:flightdataAdddetailShow="flightdataAdddetailShow"
      @refreshList="getProjectList"
      :pageType="pageType"
    />
  </div>
</template>

<script setup>
import flightdataAdd from "./flightdataAdd.vue";
import flightdataAdddetail from "./flightdataAdddetail.vue";
import { getProjectListApi } from "@/api/sales/project";
import { getProvinceApi } from "@/api/information/customer";
import { getDictsApi } from "@/api/dict";
import { ElMessage, ElMessageBox } from "element-plus";
import { getflyListApi } from "@/api/operation/flightdata.js";
// 路由
const route = useRoute();
// 搜索参数
const params = reactive({
  arrivalStatus: null, //到货状态
  checkStatus: null, // 审核状态
  includeContract: null, //是否签合同
  practiceStatus: null, //实施状态
  projectCategory: null, // 项目种类
  projectCode: "", // 项目编号
  projectName: "", // 项目名称
  projectStatus: null, // 项目状态
  projectType: null, // 	项目类型
  pageNum: 1,
  pageSize: 50,
});
// 表格加载效果
const loading = ref(false);
// 项目新增组件显隐
const flightdataAddShow = ref(false);
//详情组件的显隐
const flightdataAdddetailShow = ref(false);
//新增详情引用
const flightdataAddRef = ref(null);
//详情引用
const flightdataAdddetailRef = ref(null);
// 项目列表
const projectList = ref([]);
// 总数
const total = ref(0);
// 操作类型
const pageType = ref("");
// 审核状态列表
const checkList = ref([]);
// 是否签合同列表
const contractList = ref([]);
// 项目种类列表
const kindList = ref([]);
// 项目状态列表
const statusList = ref([]);
// 项目类型列表
const typeList = ref([]);
// 省列表
const provinceList = ref([]);
// 市列表
const cityList = ref([]);
// 区列表
const areaList = ref([]);
//飞行类型下拉
let flightTypeList = ref([]);

/**
 * @description: 重置
 */
 const handleRefresh = () => {
  Object.assign(params, {
    provinceId:null,
cityId:null,
zoneId:null,
tester:"",
projectName:"",
flyType:null,
    pageNum: 1,
    pageSize: 50,
  });
  getProjectList()
};

/**
 * @description: 获取项目列表
 */
const getProjectList = async () => {
  try {
    loading.value = true;
    let { pageNum, pageSize } = params;
    const res = await getflyListApi({ pageNum, pageSize }, params);
    projectList.value = res.rows;
    total.value = res.total;
    loading.value = false;
  } catch {
    loading.value = false;
  }
};

/**
 * @description: 表格操作
 * @param {String} type 类型
 * @param {Object} row  当前行数据
 */
let editform = ref(null);
const handleOperate = async (type, row) => {
  switch (type) {
    case "edit":
      pageType.value = type;
      flightdataAddShow.value = true;
      nextTick(() => {
        flightdataAddRef.value.handleEditInfo(row.flyId);
      });
      break;
    case "detail":
      pageType.value = type;
      flightdataAdddetailShow.value = true;
      nextTick(() => {
        flightdataAdddetailRef.value.handleDetailInfo(row.flyId);
      });
      break;
    case "delete":
      nextTick(() => {
        flightdataAddRef.value.handleEditInfo(row.flyId, "delete");
      });
      break;

    default:
      break;
  }
};

/**
 * @description: 处理省市区操作
 * @param {String} type 类型
 */
const handleProvince = (type) => {
  switch (type) {
    case "province":
      cityList.value = [];
      areaList.value = [];
      params.city = null;
      params.area = null;
      getProvinceApi(params.provinceId).then((res) => {
        if (res.code === 200) {
          cityList.value = res.data;
        }
      });
      break;
    case "city":
      areaList.value = [];
      params.area = null;
      getProvinceApi(params.cityId).then((res) => {
        if (res.code === 200) {
          areaList.value = res.data;
        }
      });
      break;
  }
};

/**
 * @description: 获取字典
 */
async function getDicts() {
  //飞行类型下拉
  await getDictsApi("fly_type").then((res) => {
    if (res.code === 200) {
      flightTypeList.value = res.data;
    }
  });

  // 省
  await getProvinceApi(1).then((res) => {
    if (res.code === 200) {
      provinceList.value = res.data;
    }
  });
}

/**
 * @description: 序号
 * @param {Number} index 索引
 */
function indexMethod(index) {
  const currentPage = params.pageNum;
  const pageSize = params.pageSize;
  const truelndex = (currentPage - 1) * pageSize + index + 1;
  return truelndex;
}

onMounted(() => {
  getProjectList();
  getDicts();
});
</script>
<style>
@import "@/assets/styles/confirmBtn.css";
</style>
<style lang="scss" scoped>
.project {
  padding: 20px;
  width: 100%;
  height: 100%;
  .search-item {
    // margin-bottom: 20px;
  }
  .pagination {
    margin-top: 20px;
  }
}
</style>
